package service;

import dao.*;
import model.*;
import utils.*;

import java.sql.*;
import java.util.List;

public class OrderService {
   private OrderDao oDao = new OrderDao();

   //生成订单
   public void addOrder(Order order) {
      Connection con = null;
      try {
         con = DataSourceUtils.getConnection();
         con.setAutoCommit(false);

         oDao.insertOrder(con, order);
         int id = oDao.getLastInsertId(con);
         order.setId(id);
         for (OrderItem item : order.getItemMap().values()) {
            oDao.insertOrderItem(con, item);
         }

         con.commit();
      } catch (SQLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
         if (con != null)
            try {
               con.rollback();
            } catch (SQLException e1) {
               // TODO Auto-generated catch block
               e1.printStackTrace();
            }
      }
   }

   //展示购物车
   public List<Order> selectAll(int userid) {
      List<Order> list = null;
      try {
         list = oDao.selectAll(userid);
         for (Order o : list) {
            List<OrderItem> l = oDao.selectAllItem(o.getId());
            o.setItemList(l);
         }
      } catch (SQLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      return list;
   }

   //获取订单商品的信息
   public Page getOrderPage(int status, int pageNumber) {
      Page p = new Page();
      p.setPageNumber(pageNumber);
      int pageSize = 10;
      int totalCount = 0;
      try {
         totalCount = oDao.getOrderCount(status);
      } catch (SQLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      p.SetPageSizeAndTotalCount(pageSize, totalCount);
      List list = null;
      try {
         list = oDao.selectOrderList(status, pageNumber, pageSize);
         for (Order o : (List<Order>) list) {
            List<OrderItem> l = oDao.selectAllItem(o.getId());
            o.setItemList(l);
         }
      } catch (SQLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      p.setList(list);
      return p;
   }

   //修改订单状态
   public void updateStatus(int id, int status) {
      try {
         oDao.updateStatus(id, status);
      } catch (SQLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }

   //从购物车删除商品
   public void delete(int id) {
      Connection con = null;
      try {
         con = DataSourceUtils.getDataSource().getConnection();
         con.setAutoCommit(false);

         oDao.deleteOrderItem(con, id);
         oDao.deleteOrder(con, id);
         con.commit();
      } catch (SQLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
         if (con != null)
            try {
               con.rollback();
            } catch (SQLException e1) {
               // TODO Auto-generated catch block
               e1.printStackTrace();
            }
      }
   }
}
